<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="chrome=1"/>
	<meta HTTP-EQUIV="Pragma" CONTENT="no-cache"/> 
	<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache"/> 
	<meta HTTP-EQUIV="Expires" CONTENT="0"/>
    <title>Yagra登录</title>
	<script type="text/javascript" src="js/HttpRequest.js"></script>
	<script type="text/javascript" src="js/Connection.js"></script>
	<script type="text/javascript" src="js/md5-min.js"></script>
	<script language="javascript">
		 function login(){
		 	var username = document.getElementById("user").value;
			var password = document.getElementById("pass").value;
			
			if (!username || !password) {
				document.getElementById("errorTip").innerHTML = "用户名和密码不能为空";
				return;
			}
			
			if(!username.match("[a-zA-Z0-9_]{3,16}")){
				document.getElementById("errorTip").innerHTML = "用户名包含非法字符";
				return;
			}
			
			var conn = new Ajax.Connection();
			var ran = conn.synGetString("yagra/get_random.py?ran="+Math.random(), null);
			ran = ran.trim()
			
			password = hex_md5(hex_md5(password)+ran);
			
			var ret = conn.synGetString("yagra/login.py?username="+username+"&password="+password, null);
			
			ret = ret.trim();
			
			if(ret === 'success'){
				window.location.href = "index.html"
			}	
			else{
				document.getElementById("errorTip").innerHTML = ret.substr(6);
			}
		}
		
		function reset(){
			document.getElementById("user").value = ''
			document.getElementById("pass").value = ''
		}
	</script>
	<style>
		body{
			text-align:center;
		}
	</style>
</head>
<body>
	<div style="font-size:42px;font-weight:bolder;font-family:'微软雅黑'">
		Yagra
	</div>
	<br/>
    <div>
    	<span>用户名</span>
		<input id="user" name='username' value=''></input>
    </div>
	<div>
		<span>密&nbsp;&nbsp;码</span>
		<input id="pass" name='password' type="password"  value=''></input>
	</div>	
	<div id="errorTip" style="color:red"></div>
	<br/>
    <button value="" onclick="login()">登录</button>
    <button value="" onclick="reset()">重置</button>
	<a href="register.html">马上注册</a>
</body>
</html>
